package com.yylending.las.personal.bandcard.verified;

import android.text.TextUtils;

import com.google.gson.Gson;
import com.yyfax.infrastructure.eventbus.EventAgent;
import com.yyfax.infrastructure.mvp.IView;
import com.yyfax.infrastructure.mvp.Presenter;
import com.yyfax.infrastructure.mvp.PresenterFactory;
import com.yyfax.infrastructure.network.entity.BaseResultEntity;
import com.yyfax.infrastructure.secure.Cryptos;
import com.yylending.las.Global;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

/**
 * P:实名认证
 * Created by ying_he on 2017/6/28.<br>
 * Copyright  2017 www.yylending.com. All Rights Reserved.<br><br>
 */

public class RealNameVerifiedPresenter extends Presenter<RealNameVerifiedModel, RealNameVerifiedPresenter.View> {

    private final static String TAG = "RealNameVerifiedPresenter";

    private static final String NO_REAL_NAME_VERIFY = "0"; // 未认证
    private static final String HAS_REAL_NAME_VERIFY = "1"; // 云贷已认证
    private static final String DO_NOT_MATCH = "2"; // 云贷与平台认证不一致

    private static final String MSG_REAL_NAME_FAILED = "获取信息失败";
    private static final String MSG_NEED_REAL_NAME = "获取额度后才能绑卡";
    private static final String MSG_DO_NOT_MATCH_REAL_NAME = "您的实名认证信息与友金所实名认证信息不一致, 请联系客服处理";

    private View mView;
    private String mRequestPage;

    public static class Generator implements PresenterFactory<RealNameVerifiedPresenter> {
        @Override
        public RealNameVerifiedPresenter create() {
            return new RealNameVerifiedPresenter();
        }
    }

    public interface View extends IView {
        void hasRealNameVerified(RealNameVerifiedModel.RealNameInfo realNameInfo);
        void noRealNameVerified(String msg);
        void noMatchRealName(String msg);
        void getRealNameVerifyFailed(String msg);
    }

	@Override
	public void attach(RealNameVerifiedPresenter.View view) {
		mRequestPage = view.getClass().getName();
		mView = view;
		mModel = new RealNameVerifiedModel();
		EventAgent.register(this);
	}

	@Override
	public void detach() {
		mView = null;
		mModel = null;
		EventAgent.unregister(this);
	}

	public void getRealNameVerify() {
		execute(null, FUNCTIONAL_CODE, false, null, mRequestPage);
	}

	@Subscribe(threadMode = ThreadMode.MAIN)
	public void realNameVerifyResult(RealNameVerifiedModel.RealNameVerifyResult result) {
		if (mRequestPage.equals(result.requestPage)) {
			if (BaseResultEntity.isResponseSuccess(result)) {
				RealNameVerifiedModel.RealNameVerify data = result.getData();
				if (TextUtils.equals(HAS_REAL_NAME_VERIFY, data.auth)) {
					RealNameVerifiedModel.DecryptJson decryptJson = new Gson().fromJson(Cryptos.aesDecrypt(data.json, Global.KEY_AES), RealNameVerifiedModel.DecryptJson.class);
					if (decryptJson != null && decryptJson.identity != null &&
							!TextUtils.isEmpty(decryptJson.identity.iname) && !TextUtils.isEmpty(decryptJson.identity.inumber)) {
						mView.hasRealNameVerified(decryptJson.identity);
					} else {
						mView.getRealNameVerifyFailed(MSG_REAL_NAME_FAILED);
					}
				} else if (TextUtils.equals(DO_NOT_MATCH, data.auth)) {
					mView.noMatchRealName(MSG_DO_NOT_MATCH_REAL_NAME);
				} else if (TextUtils.equals(NO_REAL_NAME_VERIFY, data.auth)) {
					mView.noRealNameVerified(MSG_NEED_REAL_NAME);
				} else {
					mView.getRealNameVerifyFailed(MSG_REAL_NAME_FAILED);
				}
			} else {
				mView.getRealNameVerifyFailed(MSG_REAL_NAME_FAILED);
			}
		}
	}
}
